/**
 * 
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.mlc.swing.layout.LayoutConstraintsManager;

import slhs.tempo.TableAnalyzer.IntegerField;
import slhs.tempo.scriptGenerator.PanelHandler;
import slhs.tempo.scriptGenerator.UtilityConstants;

import com.jgoodies.forms.factories.DefaultComponentFactory;

/**
 * pivotWizardPostAnalysisFilterPanelHandler.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class PivotWizardPostAnalysisFilterPanelHandler extends JPanel implements PanelHandler {

    private static final String OVERALL_ANALYSIS_FILTER_WIZARD_PANEL_LAYOUT = "OVERALL_ANALYSIS_FILTER_WIZARD_PANEL_LAYOUT.XML";

    public static PivotWizardPostAnalysisFilterPanelHandler getInstance() {
	return pivotWizardPostAnalysisFilterPanelHandler;
    }

    private JPanel pivotWizardPostAnalysisFilterPanel;

    private static final PivotWizardPostAnalysisFilterPanelHandler pivotWizardPostAnalysisFilterPanelHandler = new PivotWizardPostAnalysisFilterPanelHandler();
    // components for overall analysis
    private JLabel ignoreAboveMeanLabel;
    private JLabel ignoreBelowMeanLabel;
    private JLabel ignoreNTimeAboveStandardDeviationLabel;
    private JLabel ignoreNTimeBelowStandardDeviationLabel;
    private JLabel ignoreNTimesBelowZScoreLabel;
    private JLabel ignoreNTimesAboveZScoreLabel;

    private IntegerField ignoreNAboveMeanTimeTextField;
    private IntegerField ignoreNTimesAboveStandardDeviationTextField;
    private IntegerField ignoreNTimesAboveZScoreTextField;
    private IntegerField ignoreNTimesBelowZScoreTextField;
    private IntegerField ignoreNTimesBelowStandardDeviationTextField;
    private IntegerField ignoreNBelowMeanTimeTextField;

    private JCheckBox ignoreNAboveMeanTimeCheckBox;
    private JCheckBox ignoreNBelowMeanTimeCheckBox;
    private JCheckBox ignoreNTimesAboveStandardDeviationCheckBox;
    private JCheckBox ignoreNTimesBelowStandardDeviationCheckBox;
    private JCheckBox ignoreNTimesBelowZScoreCheckBox;
    private JCheckBox ignoreNTimesAboveZScoreCheckBox;

    // components for conditional analysis

    private Component OverallAnalysisFiltersSeperator;

    private PivotWizardPostAnalysisFilterPanelHandler() {
	if (UtilityConstants.IS_OVERALL_ANALYSIS) {
	    initOverallAnalysisComponents();
	    addOverallAnalysisComponents();
	} else {
	    initConditionalAnalysisComponents();
	    addConditionalAnalysisComponents();

	}
    }

    @Override
    public void actionPerformed(ActionEvent event) {

	Object source = event.getSource();
	if (source.equals(ignoreNAboveMeanTimeCheckBox)) {
	    ignoreNAboveMeanTimeTextField.setEnabled(ignoreNAboveMeanTimeCheckBox.isSelected());
	}
	if (source.equals(ignoreNBelowMeanTimeCheckBox)) {
	    ignoreNBelowMeanTimeTextField.setEnabled(ignoreNBelowMeanTimeCheckBox.isSelected());
	}
	if (source.equals(ignoreNTimesAboveStandardDeviationCheckBox)) {
	    ignoreNTimesAboveStandardDeviationTextField.setEnabled(ignoreNTimesAboveStandardDeviationCheckBox
		    .isSelected());
	}
	if (source.equals(ignoreNTimesBelowStandardDeviationCheckBox)) {
	    ignoreNTimesBelowStandardDeviationTextField.setEnabled(ignoreNTimesBelowStandardDeviationCheckBox
		    .isSelected());
	}
	if (source.equals(ignoreNTimesBelowZScoreCheckBox)) {
	    ignoreNTimesBelowZScoreTextField.setEnabled(ignoreNTimesBelowZScoreCheckBox.isSelected());
	}
	if (source.equals(ignoreNTimesAboveZScoreCheckBox)) {
	    ignoreNTimesAboveZScoreTextField.setEnabled(ignoreNTimesAboveZScoreCheckBox.isSelected());
	}

    }

    @Override
    public JPanel getPanel() {
	return pivotWizardPostAnalysisFilterPanel;
    }

    /**
     * 
     */
    private void addConditionalAnalysisComponents() {
	// cheat for now
	addOverallAnalysisComponents();

    }

    /**
     * 
     */
    private void addOverallAnalysisComponents() {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(OVERALL_ANALYSIS_FILTER_WIZARD_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	pivotWizardPostAnalysisFilterPanel.setLayout(panelLayout);

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimeAboveStandardDeviationLabel,
		"ignoreNTimeAboveStandardDeviationLabel");

	pivotWizardPostAnalysisFilterPanel.add(ignoreAboveMeanLabel, "ignoreAboveMeanLabel");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNBelowMeanTimeCheckBox, "ignoreNBelowMeanTimeCheckBox");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNBelowMeanTimeTextField, "ignoreNBelowMeanTimeTextField");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesAboveZScoreTextField, "ignoreNTimesAboveZScoreTextField");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNAboveMeanTimeTextField, "ignoreNAboveMeanTimeTextField");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesAboveZScoreLabel, "ignoreNTimesAboveZScoreLabel");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesAboveStandardDeviationCheckBox,
		"ignoreNTimesAboveStandardDeviationCheckBox");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesBelowZScoreCheckBox, "ignoreNTimesBelowZScoreCheckBox");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNAboveMeanTimeCheckBox, "ignoreNAboveMeanTimeCheckBox");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesBelowZScoreTextField, "ignoreNTimesBelowZScoreTextField");

	pivotWizardPostAnalysisFilterPanel.add(ignoreBelowMeanLabel, "ignoreBelowMeanLabel");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesAboveStandardDeviationTextField,
		"ignoreNTimesAboveStandardDeviationTextField");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesBelowStandardDeviationTextField,
		"ignoreNTimesBelowStandardDeviationTextField");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimeBelowStandardDeviationLabel,
		"ignoreNTimeBelowStandardDeviationLabel");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesAboveZScoreCheckBox, "ignoreNTimesAboveZScoreCheckBox");

	pivotWizardPostAnalysisFilterPanel.add(OverallAnalysisFiltersSeperator, "OverallAnalysisFiltersSeperator");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesBelowZScoreLabel, "ignoreNTimesBelowZScoreLabel");

	pivotWizardPostAnalysisFilterPanel.add(ignoreNTimesBelowStandardDeviationCheckBox,
		"ignoreNTimesBelowStandardDeviationCheckBox");

	// add action listeners
	ignoreNAboveMeanTimeCheckBox.addActionListener(this);
	ignoreNBelowMeanTimeCheckBox.addActionListener(this);
	ignoreNTimesAboveStandardDeviationCheckBox.addActionListener(this);
	ignoreNTimesBelowStandardDeviationCheckBox.addActionListener(this);
	ignoreNTimesBelowZScoreCheckBox.addActionListener(this);
	ignoreNTimesAboveZScoreCheckBox.addActionListener(this);

    }

    /**
     * 
     */
    private void initConditionalAnalysisComponents() {
	// Cheat for now
	initOverallAnalysisComponents();
    }

    /**
     * 
     */
    private void initOverallAnalysisComponents() {
	// here are declarations for the controls you created
	pivotWizardPostAnalysisFilterPanel = new JPanel();
	ignoreNTimeAboveStandardDeviationLabel = new JLabel("times Standard Deviation");
	ignoreAboveMeanLabel = new JLabel("sec.");
	ignoreNBelowMeanTimeCheckBox = new JCheckBox("Ignore Reaction Times Below");
	ignoreNBelowMeanTimeTextField = new IntegerField();
	ignoreNTimesAboveZScoreTextField = new IntegerField();
	ignoreNAboveMeanTimeTextField = new IntegerField();
	ignoreNTimesAboveZScoreLabel = new JLabel("times Z-score");
	ignoreNTimesAboveStandardDeviationCheckBox = new JCheckBox("Ignore Reaction Times Above");
	ignoreNTimesBelowZScoreCheckBox = new JCheckBox("Ignore Reaction Times Below");
	ignoreNAboveMeanTimeCheckBox = new JCheckBox("Ignore Reaction Times Above");
	ignoreNTimesBelowZScoreTextField = new IntegerField();
	ignoreBelowMeanLabel = new JLabel("sec.");
	ignoreNTimesAboveStandardDeviationTextField = new IntegerField();
	ignoreNTimesBelowStandardDeviationTextField = new IntegerField();
	ignoreNTimeBelowStandardDeviationLabel = new JLabel("times Standard Deviation");
	ignoreNTimesAboveZScoreCheckBox = new JCheckBox("Ignore Reaction Times Above");
	OverallAnalysisFiltersSeperator = DefaultComponentFactory.getInstance().createSeparator(
		"Overall Analysis Filters", SwingConstants.LEFT);
	ignoreNTimesBelowZScoreLabel = new JLabel("times Z-Score");
	ignoreNTimesBelowStandardDeviationCheckBox = new JCheckBox("Ignore Reaction Times Below");

	// configure components
	ignoreNAboveMeanTimeTextField.setEnabled(false);
	ignoreNBelowMeanTimeTextField.setEnabled(false);
	ignoreNTimesAboveStandardDeviationTextField.setEnabled(false);
	ignoreNTimesBelowStandardDeviationTextField.setEnabled(false);
	ignoreNTimesBelowZScoreTextField.setEnabled(false);
	ignoreNTimesAboveZScoreTextField.setEnabled(false);

    }

    public String getIgnoreNAboveMeanTime() {
	return ignoreNAboveMeanTimeTextField.getText();
    }

    public String getIgnoreNBelowMeanTime() {
	return ignoreNBelowMeanTimeTextField.getText();
    }

    public String getIgnoreNTimesAboveStandardDeviation() {
	return ignoreNTimesAboveStandardDeviationTextField.getText();
    }

    public String getIgnoreNTimesBelowStandardDeviation() {
	return ignoreNTimesBelowStandardDeviationTextField.getText();
    }

    public String getIgnoreNTimesAboveZScore() {
	return ignoreNTimesAboveZScoreTextField.getText();
    }

    public String getIgnoreNTimesBelowZScore() {
	return ignoreNTimesBelowZScoreTextField.getText();
    }

}
